package com.tl.sun.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.utils.EmptyUtils;
import com.blankj.utilcode.utils.RegexUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.tl.sun.AppContext;
import com.tl.sun.OkHeader;
import com.tl.sun.R;
import com.tl.sun.api.network.StringDialogCallback;
import com.tl.sun.model.BaseModel;
import com.tl.sun.ui.interfaces.VerifyHelper;
import com.tl.sun.utils.ToastUtil;
import com.tl.sun.widgets.ButtonTimer;

import org.json.JSONObject;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by apple on 16/10/12.
 */

public class ForGetActivity extends BaseActivity {


    @Bind(R.id.tv_generic_title)
    TextView mTvGenericTitle;
    @Bind(R.id.et_forget_phone)
    EditText mEtForgetPhone;
    @Bind(R.id.et_forget_code)
    EditText mEtForgetCode;
    @Bind(R.id.bt_send_code)
    ButtonTimer mBtSendCode;
    @Bind(R.id.et_forget_password)
    EditText mEtForgetPassword;
    @Bind(R.id.et_forget_pass_aign)
    EditText mEtForgetPassAign;
    @Bind(R.id.tv_forget_submit)
    TextView mTvForgetSubmit;
    @Bind(R.id.tv_forget_login)
    TextView mTvForgetLogin;
    @Bind(R.id.tv_forget_regist)
    TextView mTvForgetRegist;
    @Bind(R.id.rl_generic_back)
    RelativeLayout mRlGenericBack;
    @Bind(R.id.iv_forget_verfiy)
    ImageView mIvForgetVerfiy;
    @Bind(R.id.et_forget_verfiy)
    EditText mEtForgetVerfiy;


    private String num;
    private String code;
    private String newPass;
    private String aginPass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        ButterKnife.bind(this);
        init();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
    }

    @Override
    protected void onStart() {
        super.onStart();

        VerifyHelper.getImgVerify(mIvForgetVerfiy);
    }

    public void init() {
        mTvGenericTitle.setText("忘记密码");
    }

    @OnClick({R.id.rl_generic_back, R.id.bt_send_code, R.id.tv_forget_submit, R.id.tv_forget_login
            , R.id.tv_forget_regist, R.id.iv_forget_verfiy})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_send_code:
                num = mEtForgetPhone.getText().toString();
                String verfiy = mEtForgetVerfiy.getText().toString();
                if (!TextUtils.isEmpty(num)) {
                    if (RegexUtils.isMobileExact(num)) {
                        if (EmptyUtils.isNotEmpty(verfiy)) {
                            getCode(num, verfiy);
                        } else {
                            ToastUtil.showToast(getString(R.string.et_is_verify_empty));
                        }
                    } else {
                        ToastUtil.showToast("手机号格式不正确");
                    }
                } else {
                    ToastUtil.showToast("请输入绑定手机号");
                }
                break;
            case R.id.tv_forget_submit:
                num = mEtForgetPhone.getText().toString();
                code = mEtForgetCode.getText().toString();
                newPass = mEtForgetPassword.getText().toString();
                aginPass = mEtForgetPassAign.getText().toString();
                if (!TextUtils.isEmpty(num)) {
                    if (RegexUtils.isMobileExact(num)) {
                        if (!TextUtils.isEmpty(code)) {
                            if (!TextUtils.isEmpty(newPass)) {
                                if (newPass.equals(aginPass)) {
                                    forGetPasswrod(num, code, newPass);
                                } else {
                                    ToastUtil.showToast("两次输入密码不一致");
                                }
                            } else {
                                ToastUtil.showToast("请输入新密码");
                            }
                        } else {
                            ToastUtil.showToast("请输入验证码");
                        }
                    } else {
                        ToastUtil.showToast("手机号格式不正确");
                    }
                } else {
                    ToastUtil.showToast("请输入绑定手机号");
                }

                break;
            case R.id.tv_forget_login:
                finish();
                break;
            case R.id.iv_forget_verfiy:
                VerifyHelper.getImgVerify(mIvForgetVerfiy);
                break;
            case R.id.tv_forget_regist:
                break;
            case R.id.rl_generic_back:
                onBackPressed();
                break;
        }
    }


    public void forGetPasswrod(String num, String code, String pass) {
        HashMap<String, String> map = new HashMap<>();
        map.put("num", num);
        map.put("password", pass);
        map.put("code", code);
        JSONObject json = new JSONObject(map);

        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(this)).post(AppContext.getInstance().getBaseUrl() + "account/forget_pwd")
                .tag(this)
                .upJson(json.toString())
                .execute(new StringDialogCallback(ForGetActivity.this, "加载中...", true) {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        BaseModel baseModel = gson.fromJson(s, BaseModel.class);
                        if (baseModel.getRet().equals("0")) {
                            ToastUtil.showToast(baseModel.getMsg());
                            Intent success = new Intent(ForGetActivity.this, ForGetSuccessActivity.class);
                            startActivity(success);
                            finish();
                        } else {
                            VerifyHelper.getImgVerify(mIvForgetVerfiy);
                            ToastUtil.showToast(baseModel.getMsg());
                        }
                    }
                });
    }

    public void getCode(String num, String verfiy) {
        HashMap<String, String> map = new HashMap<>();
        map.put("num", num);
        map.put("code", "fgt_pass");
        map.put("verify", verfiy);
        JSONObject json = new JSONObject(map);

        OkGo.getInstance().addCommonHeaders(OkHeader.getCommonHeadToken(this)).post(AppContext.getInstance().getBaseUrl() + "account/get_code_new")
                .tag(ForGetActivity.this)
                .upJson(json.toString())
                .execute(new StringDialogCallback(ForGetActivity.this, "加载中...", true) {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson = new Gson();
                        BaseModel baseModel = gson.fromJson(s, BaseModel.class);
                        if (baseModel.getRet().equals("0")) {
                            mBtSendCode.setTimes(60);
                            if (!mBtSendCode.isRun()) {
                                AppContext.timenubmer = 6;
                                mBtSendCode.beginRun();
                                mBtSendCode.setEnabled(false);
                            }
                            ToastUtil.showToast(baseModel.getMsg());
                        } else {
                            VerifyHelper.getImgVerify(mIvForgetVerfiy);
                            ToastUtil.showToast(baseModel.getMsg());
                        }
                    }
                });

    }

}
